package ssm.blog.controller.onstage;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import ssm.blog.entity.Album;
import ssm.blog.entity.Blog;
import ssm.blog.entity.BlogType;
import ssm.blog.entity.Quotation;
import ssm.blog.service.AlbumService;
import ssm.blog.service.BlogService;
import ssm.blog.service.BlogTypeService;
import ssm.blog.service.QuotationService;
import ssm.blog.util.MD5Util;
import ssm.blog.util.ResponseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: lyon
 * @description: 首界面-控制层
 * @author: Lyon
 * @create: 2018-05-13 23:06
 **/
@Controller
@RequestMapping("/onstage")
public class HomeController {
    @Resource
    BlogTypeService blogTypeService;
    @Resource
    BlogService blogService;
    @Resource
    QuotationService quotationService;
    @Resource
    AlbumService albumService;
    @RequestMapping("home.html")
    String home(HttpServletResponse response, Model model,
                @RequestParam(defaultValue = "true")Boolean down,
                @RequestParam(defaultValue = "false")Boolean img,
                @RequestParam(defaultValue = "")String keyword,
                @RequestParam(defaultValue = "-1")Integer type_id,
                @RequestParam(defaultValue = "0")Integer pageNum,
                @RequestParam(defaultValue = "4") Integer pageSize
                ){
        if(img){
            pageSize=4;
        }else{
            pageSize=8;
        }
        List<BlogType>blogTypes=blogTypeService.list();
        model.addAttribute("blogTypes",blogTypes);
        PageHelper.startPage(pageNum,pageSize);
        List<Blog>blogs=blogService.listBlogs(1,type_id,keyword,down);
        PageInfo<Blog>blogPageInfo=new PageInfo<>(blogs);
        PageHelper.startPage(0,4);
        List<Blog>rank=blogService.listRank(1);
        model.addAttribute("type_id",type_id);
        model.addAttribute("keyword",keyword);
        model.addAttribute("blogs",blogs);
        model.addAttribute("blogPageInfo",blogPageInfo);
        model.addAttribute("rank",rank);
        model.addAttribute("img",img);
        model.addAttribute("down",down);
        return "interface/onstage/home";
    }

    @RequestMapping("aboutMe.html")
    String aboutMe(HttpServletResponse response){
        return "interface/onstage/aboutMe";
    }

    @RequestMapping("contactMe.html")
    String contactMe(HttpServletResponse response){
        return "interface/onstage/contactMe";
    }
    @RequestMapping("albums.html")
    String albums(HttpServletResponse response,Model model){
        List<Album> albums=albumService.list();
        model.addAttribute("albums",albums);
        return "interface/onstage/albums";
    }

    @RequestMapping("single.html")
    String single(HttpServletResponse response,Integer id,Model model,Integer type_id){
        if(type_id==null){
            type_id=new Integer(-1);
        }
        Blog blog=blogService.selectById(id);
        model.addAttribute("blog_single",blog);
        model.addAttribute("type_id",type_id);
        List<Blog>next=blogService.getNext(id,1,type_id);
        if(next==null||next.size()==0){
            model.addAttribute("hasNext",false);
        }else{
            model.addAttribute("hasNext",true);
            model.addAttribute("nextId",next.get(0).getId());
        }
        List<Blog>pre=blogService.getPre(id,1,type_id);
        if(pre==null||pre.size()==0){
            model.addAttribute("hasPre",false);
        }else{
            model.addAttribute("hasPre",true);
            model.addAttribute("preId",pre.get(0).getId());
        }
        return "interface/onstage/single";
    }

    @RequestMapping("quotations.html")
    String quotations(Model model,
                      @RequestParam(defaultValue = "true")Boolean down,
                      @RequestParam(defaultValue = "false")Boolean img,
                      @RequestParam(defaultValue = "0")Integer pageNum,
                      @RequestParam(defaultValue = "20") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Quotation>quotations=quotationService.list(down);
        PageInfo<Quotation>pageInfo=new PageInfo<>(quotations);
        model.addAttribute("quotations",quotations);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("img",img);
        model.addAttribute("down",down);
        return "interface/onstage/quotations";
    }
}
